package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.PermissionService;
import com.itheima.service.RoleService;
import com.itheima.service.UserService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;
    @Reference
    private static PermissionService permissionService;

    @Reference
    private static RoleService roleService;

    //根据用户名获取动态菜单展示
    @RequestMapping("/getMenuList")
    public Result getMenuList( String username){
        try{
            List<Menu> list = userService.getMenuList(username);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,list);
        }catch (Exception e){
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }
    //获取当前登录（认证）用户的用户名
    @RequestMapping("/getLoginUsername")
    public Result getLoginUsername(){
        try{
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String username = user.getUsername();
            String password = user.getPassword();
            Collection<GrantedAuthority> authorities = user.getAuthorities();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
        }catch (Exception e){
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }

    @RequestMapping("add")
    @ResponseBody
    public Result add(@RequestBody com.itheima.pojo.User user){
        try {
            this.userService.add(user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新增用户失败");
        }
        return new Result(true,"新增用户成功");
    }

    @RequestMapping("findPage")
    @ResponseBody
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        return userService.findPage(queryPageBean);
    }

    @RequestMapping("edit")
    @ResponseBody
    public Result edit(@RequestBody  com.itheima.pojo.User user) {
        return userService.edit(user);
    }

    @RequestMapping("delete")
    @ResponseBody
    public Result delete(@RequestBody com.itheima.pojo.User user) {
        return userService.deleteById(user);
    }

    @RequestMapping("findAll")
    @ResponseBody
    public Result findAll() {
        return userService.findAll1();
    }

    @RequestMapping("findById")
    @ResponseBody
    public Result findById(Integer id) {
        return userService.findById(id);
    }

    @RequestMapping("findRole")
    @ResponseBody
    public Result findRole(){
        return userService.findRole();
    }


    @RequestMapping("/updatePassword")
    public Result updatePassword(@RequestParam String username, @RequestBody Map form){
        try{
            String oldPassword = form.get("password").toString();
            String newPassword = form.get("newPassword").toString();

            Boolean flag = userService.updatePassword(username, oldPassword, newPassword);
            if (flag==true){
                return new Result(true, "密码修改成功");
            }
            return new Result(false,"密码修改失败");
        }catch (Exception e){
            return new Result(false, "");
        }
    }
}
